import pygame

class BgSprite(pygame.sprite.Sprite):
    def __init__(self,image_name,top,speed=5):
        super().__init__()
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.rect.top = top
        self.speed = speed

    def update(self):
        self.rect.top += self.speed
        if self.rect.top >= 600:
            self.rect.top = -600
class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(size=(800,600))
        self.init_title_logo()

        # 创建背景精灵组 将两个精灵实例加入背景精灵组
        self.bg_group = pygame.sprite.Group()

        self.bg = BgSprite("bg.png.jpg",0)
        self.bg2 = BgSprite("bg.png.jpg",-600)
        self.bg.add(self.bg_group)
        self.bg2.add(self.bg_group)

        self.clock = pygame.time.Clock()

    def init_title_logo(self):
        pygame.display.set_caption("我的游戏")
        icon_surface = pygame.image.load("bg.png.jpg")
        pygame.display.set_icon(icon_surface)

    def init_event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

    def updata_draw(self):

        # 绘制精灵组到一个面  并且更新精灵组
        self.bg_group.draw(self.screen)
        self.bg_group.update()
        pygame.display.flip()

    def run(self):
        while True:
            self.clock.tick(20)
            self.init_event()
            self.updata_draw()


game = Game()
game.run()